package cn.feichanghao.nutz.quickstart;

import java.lang.reflect.Method;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.RequestPath;
import org.nutz.mvc.View;

import cn.feichanghao.nutz.quickstart.auth.model.Site;

public class SiteFilter implements ActionFilter {

	private static final Log log = Logs.getLog(SiteFilter.class);
	private final String MENU_MATCH = "^.+\\.(jsp)$";
	private Pattern matchPtn;

	public static String getAppPath(HttpServletRequest req) {
		StringBuffer appPath = new StringBuffer();
		appPath.append("http://").append(req.getServerName());
		if (req.getServerPort() != 80) {
			appPath.append(":").append(req.getServerPort());
		}
		return appPath.toString();
	}

	@Override
	public View match(ActionContext ac) {
		String regx = MENU_MATCH;
		if (!"null".equalsIgnoreCase(regx)) {
			matchPtn = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);

			//RequestPath path = Mvcs.getRequestPathObject((HttpServletRequest) req);
			if (null == matchPtn || !matchPtn.matcher(ac.getPath()).find()) {

				Site site=new Site();
				site.setName("NutzQuickStart 快速入门");
				site.setFavicon("favicon.ico");
				site.setDesc("NutzQuickStart.Maven快速入门");
				site.setKeyword("Nutz,QuickStart,快速入门");
				site.setTheme("default");
				site.setUrl("http://code.google.com/p/nutz-quickstart/");
				site.setVersion("0.1.3");
				
				ac.getRequest().setAttribute("site", site);

				
				ac.getRequest().setAttribute("reqpath", getAppPath(ac.getRequest()));
				log.debug("完成网站选项设置");
			}
		}
		return null;
	}

}